package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.activityuser.model.AddActivityUserModel;
import com.ls.controller.activityuser.model.EditActivityUserModel;
import com.ls.controller.activityuser.model.updateStatusModel;
import com.ls.entity.ActivityUser;
import com.ls.entity.Customer;
import com.ls.entity.StartBanner;
import com.ls.mapper.ActivityUserMapper;
import com.ls.service.IActivityUserService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ls.constant.SystemConstants.NUMBER_1;

@Service
@Slf4j
@RequiredArgsConstructor
public class ActivityUserServiceImpl extends ServiceImpl<ActivityUserMapper, ActivityUser> implements IActivityUserService {
    /** 活动对象列表 */
    @Override
    public ResultData pageList(Integer validFlag) {
        List<ActivityUser> list = list(new LambdaQueryWrapper<ActivityUser>()
                .eq(validFlag!=null,ActivityUser::getValidFlag,validFlag)
                .orderByDesc(ActivityUser::getCreateTime));
        return ResultData.successData(list);
    }
    /** 新增活动对象 */
    @Override
    public ResultData add(AddActivityUserModel model) {
        ActivityUser activityUser = new ActivityUser();
        BeanUtils.copyProperties(model, activityUser);
        activityUser.setValidFlag(NUMBER_1);
        return save(activityUser) ?
                ResultData.successMsg("新增成功") :
                ResultData.failure("新增失败");
    }
    /** 编辑活动对象 */
    @Override
    public ResultData edit(EditActivityUserModel model) {
        ActivityUser activityUser = getById(model.getAuId());
        if (activityUser == null){
            return ResultData.failure("活动对象不存在");
        }
        BeanUtils.copyProperties(model, activityUser);
        activityUser.setUpdateTime(LocalDateTime.now());
        return updateById(activityUser) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }
    /** 修改活动对象启用状态 */
    @Override
    public ResultData updateStatus(updateStatusModel model) {
        ActivityUser activityUser = getById(model.getAuId());
        if (activityUser == null){
            return ResultData.failure("活动对象不存在");
        }
        BeanUtils.copyProperties(model, activityUser);
        activityUser.setUpdateTime(LocalDateTime.now());
        return updateById(activityUser) ?
                ResultData.successMsg("修改成功") :
                ResultData.failure("修改失败");
    }
}
